package cn.bgs.serviceedu.service.impl;

import cn.bgs.serviceedu.dao.VideoMapper;
import cn.bgs.serviceedu.entity.Video;
import cn.bgs.serviceedu.service.VideoService;
import cn.bgs.serviceedu.vo.RedisList;
import cn.bgs.serviceedu.vo.RedisTop;
import cn.bgs.serviceedu.vo.VodList;
import cn.bgs.serviceutils.utils.CSException;
import cn.bgs.serviceutils.utils.RedisUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author YiChen
 * @since 2021-11-16
 */
@Service
public class VideoServiceImpl extends ServiceImpl<VideoMapper, Video> implements VideoService {

    @Autowired
    private RedisUtils redisUtils;
    @Override
    public List<VodList> getRedisList() {
        //查询所有医生
        ArrayList<VodList> vodLists = null;
        try {
            Set<String> key = redisUtils.scan("video");
            Iterator<String> iterator = key.iterator();
            //创建集合存入 id 和 val
            vodLists = new ArrayList<>();
            while (iterator.hasNext()){
                String next = iterator.next();
                String[] split = next.split(":");
                String id =split[1];
                int val = (int) redisUtils.get(next);
                VodList redisList = new VodList(id,val);
                vodLists.add(redisList);
            }
        } catch (Exception e) {
            throw new CSException(20001,"视频遍历失败");
        }
        return vodLists;
    }
}
